using Koala.Pro.Common;
using System.Globalization;
using System.Windows.Data;

namespace Koala.Pro.Helpers;

public class NumberToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ConvertToBoolean(value);
    }

    private bool? ConvertToBoolean(object value)
    {
        if (value == null || value.ToString().IsNullOrEmpty())
        {
            return null;
        }
        bool value2;
        if (!(value is float num))
        {
            if (!(value is double num2))
            {
                if (!(value is int num3))
                {
                    if (!(value is long num4))
                    {
                        throw new NotSupportedException($"value must be a number type, got {value?.GetType()}");
                    }
                    value2 = (float)num4 != 0f;
                }
                else
                {
                    value2 = num3 != 0;
                }
            }
            else
            {
                value2 = num2 != 0.0;
            }
        }
        else
        {
            value2 = num != 0f;
        }
        return value2;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || value.ToString().IsNullOrEmpty())
        {
            return DBNull.Value;
        }
        return ((bool)value) ? 1 : 0;
    }
}
